import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from DBUtils import DBUtils


class EditCardOperation:
    def editcard_name(self, driver, username, password, bankname):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        time.sleep(1)
        # 选择一张银行卡，点击编辑
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[1]').click()
        time.sleep(1)
        # 输入新的、合法的银行名
        driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]').clear()
        driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]').send_keys(bankname)
        # 点击更新按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_update_btn"]').click()
        time.sleep(1)

    def editcard_num(self, driver, username, password, num):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        time.sleep(1)
        # 选择一张银行卡，点击编辑
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[1]').click()
        time.sleep(1)
        # 在卡号输入框中输入银行卡号
        driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').clear()
        driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').send_keys(num)
        # 点击更新按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_update_btn"]').click()
        time.sleep(1)

    def editcard_illegalnum(self, driver, username, password, illegal_num):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        time.sleep(1)
        # 选择一张银行卡，点击编辑
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[1]').click()
        time.sleep(1)
        # 输入新的、不合法的银行卡号
        driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').clear()
        driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').send_keys(illegal_num)
        # 点击更新按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_update_btn"]').click()
        time.sleep(1)

    def editcard_type(self, driver, username, password, cardtype):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        time.sleep(1)
        # 选择一张银行卡，点击编辑
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[1]').click()
        time.sleep(1)
        # 点击另一个类型按钮
        driver.find_element(By.XPATH, '//*[@id="bankCardUpdateModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]').click()
        # 点击更新按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_update_btn"]').click()
        time.sleep(1)

    def editcard_name_result(self, id, bankname):
        time.sleep(1)
        dbutils = DBUtils()
        now_name = dbutils.select(f'select cardBank from bankcard where id = {id}')[0][0]
        if now_name == bankname:
            return 1
        else:
            return 0

    def editcard_num_result(self, id, cardnum):
        time.sleep(1)
        dbutils = DBUtils()
        now_num = dbutils.select(f'select cardNum from bankcard where id = {id}')[0][0]
        if now_num == cardnum:
            return 1
        else:
            return 0

    def editcard_illnum_result(self, id, cardnum):
        time.sleep(1)
        dbutils = DBUtils()
        now_num = dbutils.select(f'select cardNum from bankcard where id = {id}')[0][0]
        if now_num == cardnum:
            return 1
        else:
            return 0

    def editcard_type_result(self, id, cardtype):
        time.sleep(1)
        dbutils = DBUtils()
        now_type = dbutils.select(f'select type from bankcard where id = {id}')[0][0]
        if now_type == cardtype:
            return 1
        else:
            return 0
